package com.vgemv.jsutilitysdk.InputBar.JsInputBox;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.vgemv.jsutilitysdk.JsButton.JsButton;
import com.vgemv.jsutilitysdk.JsColorManager.JsColorManager;
import com.vgemv.jsutilitysdk.JsKeyboardManager.JsKeyboardManager;
import com.vgemv.jsutilitysdk.JsThread.JsThreadManager;

public class JsInputBoxBet extends RelativeLayout {

    public EditText met;
    public JsButton mBtn;
    public JsInputBoxBetVm mInputBoxBetVm;
    private int setupUiCount = 0;
    public JsInputBoxCallback mCallback;

    public JsInputBoxBet(Context context) {
        super(context);
        setId(View.generateViewId());
        setBackgroundColor(Color.argb(255,255,255,255));
        mInputBoxBetVm = new JsInputBoxBetVm();
        setupUi();
    }

    public void setupUiIfNeeded() {
        setupUiCount++;
        if(setupUiCount == 1) {
            JsThreadManager.runInMainThreadAtNextRunloop(new Runnable() {
                @Override
                public void run() {
                    setupUi();
                }
            });
        }
    }

    private void setupUi() {
        setupUi(mInputBoxBetVm);
    }

    private void setupUi(JsInputBoxBetVm vm) {
            //met EditText
            {
                if (met == null) {
                    met = new EditText(this.getContext());
                    met.setId(View.generateViewId());
                    met.setHint("写评论...");
                    //设置了背景色,就会去除默认的底部横线
                    met.setBackgroundColor(JsColorManager.grayColorMax255(245));
                    met.setPadding(20,0,20,0);
                    met.setHintTextColor(Color.argb(255,180,180,180));
                    met.setTextSize(mInputBoxBetVm.textSize);
                    met.setMinHeight(JsInputBoxBetVm.minTextHeight);
                    met.setMaxHeight(JsInputBoxBetVm.maxTextHeight);
                    met.setShowSoftInputOnFocus(true);
                    addView(met);

                    met.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            mInputBoxBetVm.text = charSequence.toString();
                            if(charSequence.length()==0) {
                                if(mInputBoxBetVm.isEmpty == false) {
                                    mInputBoxBetVm.isEmpty = true;
                                    setupUiIfNeeded();
                                }
                            }
                            else  {
                                if(mInputBoxBetVm.isEmpty == true) {
                                    mInputBoxBetVm.isEmpty = false;
                                    setupUiIfNeeded();
                                }
                            }
                        }

                        @Override
                        public void afterTextChanged(Editable editable) {

                        }
                    });
                }

                //根据字符串的高度来设置,如果<minHeight则为minHeight
                //如果>maxHeight,则为maxHeight;
//                int textHeight = JsGeometryManager.getOneLineTextHeight(getContext(),vm.text,vm.textSize);
//                JsLogManager.logc_d("JsInputBoxBeta","textHeight is %d",textHeight);
//                if(textHeight < JsInputBoxBetVm.minTextHeight) {
//                    textHeight = JsInputBoxBetVm.minTextHeight;
//                }
//                else if(textHeight > JsInputBoxBetVm.maxTextHeight) {
//                    textHeight = JsInputBoxBetVm.maxTextHeight;
//                }
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                lp.setMargins(30, 30, 30, 30);
                met.setLayoutParams(lp);

                //根据vm的内容显示met
//                met.setText(mInputBoxBetVm.text);
            }

            //publish 发布按钮
            {
                if(mBtn == null) {
                    mBtn = new JsButton(getContext());
                    mBtn.mVm.mi_height = 80;
                    mBtn.mVm.mi_width = 160;
                    mBtn.setId(View.generateViewId());
                    addView(mBtn);

                    mBtn.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View view) {
                            if(!mInputBoxBetVm.isEmpty) {
                                if(mCallback != null) {
                                    mCallback.jsInputBoxClickPublish(JsInputBoxBet.this,mInputBoxBetVm.text);
                                }
                                mInputBoxBetVm.text = "";
                                setupUiIfNeeded();
                            }
                        }
                    });
                }

                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(mBtn.mVm.mi_width, mBtn.mVm.mi_height);
                lp.addRule(RelativeLayout.BELOW,met.getId());
                lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                lp.setMargins(0,0,30,30);
                mBtn.setLayoutParams(lp);

                mBtn.mVm.mI_borderWidth = 4;
                mBtn.mVm.mI_borderColor = JsColorManager.grayColorMax255(30);



                if(mInputBoxBetVm.isEmpty) {
                    mBtn.mVm.mI_borderColor = JsInputBoxBetVm.emptyColor;
                    mBtn.mVm.mi_textColor = JsInputBoxBetVm.emptyColor;
                }
                else {
                    mBtn.mVm.mi_textColor = mBtn.mVm.mI_borderColor = JsInputBoxBetVm.notEmptyColor;
                }
                mBtn.setText("发布",mInputBoxBetVm.publishButtonTextSize);


            }
            setupUiCount = 0;
    }

    //获取焦点并手动开启键盘
    public void getFocusAndShowKeyboard() {
        met.requestFocus();
        JsKeyboardManager.showKeyboard(met,getContext());
    }

    public void clear() {
        mInputBoxBetVm.text = "";
        met.setText("");
        mInputBoxBetVm.isEmpty = true;
        setupUiIfNeeded();
    }
}
